#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
long long num[maxn];
long long dp[maxn];
void pre_num(int n)
{
    memset(dp, 0, sizeof(dp));
    num[0] = 0;
    for (int i = 1; i <= n; i++)
        scanf("%d", &num[i]);
}

void get_ans(int n)
{
    long long Max = -1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            if (num[j] < num[i])
                dp[i] = max(dp[i], dp[j] + num[i]); //之前数比当前数小，就加上当前数到之前数的和上面
        }
        dp[i] = max(dp[i], num[i]); //之前数都不比当前数小的情况下直接就是当前数
        if (dp[i] > Max)            //记录最大的那个和
            Max = dp[i];
    }

    printf("%lld\n", Max);
}
int main()
{
    int n;
    while (scanf("%d", &n) && n)
    {
        pre_num(n);
        get_ans(n);
    }
}
// https: //blog.csdn.net/yopilipala/article/details/74372746